package com.dzy.chiyan.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.RecyclerView
import com.dzy.chiyan.activity.ChatActivity
import com.dzy.chiyan.R
import com.dzy.chiyan.data.DBHelper
import com.dzy.chiyan.data.FriendshipDAOImpl

// 显示好友信息，也可以拉黑好友(坏笑
class FriendAdapter(private val friendList: MutableList<Friend>, val userID: Int) :
    RecyclerView.Adapter<FriendAdapter.FriendViewHolder>() {
    private lateinit var context: Context
    class FriendViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val nameTextView: TextView = itemView.findViewById(R.id.nameTextView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FriendViewHolder {
        context = parent.context
        val view = LayoutInflater.from(context).inflate(R.layout.item_friend, parent, false)
        return FriendViewHolder(view)
    }

    override fun onBindViewHolder(holder: FriendViewHolder, position: Int) {
        val friend = friendList[position]

        holder.nameTextView.text = friend.name

        //点击item传入friendId然后跳转到聊天页面
        holder.itemView.setOnClickListener {
            val intent = Intent(context, ChatActivity::class.java).apply {
                putExtra("friendId", friend.id)
                putExtra("userID", userId)
            }
            context.startActivity(intent)
        }
        // 长按弹出删除好友对话框
        holder.itemView.setOnLongClickListener {
            showDeleteConfirmationDialog(holder.adapterPosition)
            true
        }
    }

    override fun getItemCount() : Int = friendList.size

    private fun showDeleteConfirmationDialog(position: Int) {
        val alertDialog = AlertDialog.Builder(context)
            .setTitle("删除好友")
            .setMessage("您确定要删除该好友吗？/(ㄒoㄒ)/~~")
            .setPositiveButton("确定") {  _, _ ->
                // 删除对应的数据
                val del = friendList[position].friendshipID
                friendList.removeAt(position)
                notifyItemRemoved(position)
                //删除数据库对应好友信息
                val dao = FriendshipDAOImpl(DBHelper(context))
                val message: String
                message = if (dao.delete(del)) {
                    "删除成功"
                } else {
                    "删除失败"
                }
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
            }
            .setNegativeButton("取消", null)
            .create()
        alertDialog.show()
    }
    // 添加好友
    fun addFriend(friend: Friend) {
        friendList.add(friend)
        notifyItemInserted(friendList.size - 1)
    }
    // 重新加载好友的数据
    fun reloadData(newFriendList: List<Friend>) {
        friendList.clear()
        friendList.addAll(newFriendList)
        notifyDataSetChanged()
    }
}


